$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'schedule/Select',
        datatype: "json",
        colModel: [
            { label: '第几节', name: 'course_index', width: 45, key: true },
            { label: '上课时间', name: 'start', width: 75 },
            { label: '下课时间', name: 'end', width: 90 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            course_index: null
        },
        showList: true,
        title:null,
        schedule:{
            status:1
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.schedule = {status:1};
        },
        update: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getUser(userId);
        },
        del: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "schedule/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.schedule.course_index == null ? "schedule/Insert" : "schedule/Update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.schedule),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getUser: function(userId){
            $.get(baseURL + "schedule/info/"+userId, function(r){
                vm.schedule = r.schedule;
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'course_index': vm.q.course_index},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.schedule.course_index)){
                alert("第几节不能为空");
                return true;
            }

            if(isBlank(vm.schedule.start)){
                alert("开始时间不能为空");
                return true;
            }

            if(isBlank(vm.schedule.end)){
                alert("结束时间不能为空");
                return true;
            }
        }
    }
});